﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.GISClient;
using ESRI.ArcGIS.esriSystem;
using System.Threading;
using System.Windows.Forms;
using ESRI.ArcGIS.Server;
/*
 * 这个类没用了
 */
namespace ClientApplication
{
    public class GISServerMng
    {

        public GISServerMng(string host, string username, string password) 
        {
            this.Host = host;
            this.Username = username;
            this.Password = password;
        }
        List<IAGSServerObjectName> pList = null;

        public List<IAGSServerObjectName> PList
        {
            get { return pList; }
            set { pList = value; }
        }

        private string host;

        public string Host
        {
            get { return host; }
            set { host = value; }
        }
        private string username;

        public string Username
        {
            get { return username; }
            set { username = value; }
        }
        private string password;

        public string Password
        {
            get { return password; }
            set { password = value; }
        }

        /// <summary>
        /// 获得IServerObjectAdmin对象
        /// </summary>
        /// <returns></returns>
        public IServerObjectAdmin GetObjAdmin() 
        {
            try
            {
                IPropertySet propertySet = new PropertySetClass();
                propertySet.SetProperty("url", Host);
                propertySet.SetProperty("ConnectionMode", esriAGSConnectionMode.esriAGSConnectionModeAdmin);
                propertySet.SetProperty("ServerType", esriAGSServerType.esriAGSServerTypeDiscovery);
                propertySet.SetProperty("user", Username);
                propertySet.SetProperty("password", Password);
                propertySet.SetProperty("ALLOWINSECURETOKENURL", true); //设置为false会弹出一个警告对话框

                IAGSServerConnectionName3 pConnectName = new AGSServerConnectionNameClass() as IAGSServerConnectionName3;//10.1新增接口
                pConnectName.ConnectionProperties = propertySet;

                IAGSServerConnectionAdmin pAGSAdmin = ((IName)pConnectName).Open() as IAGSServerConnectionAdmin;


                return pAGSAdmin.ServerObjectAdmin;
            }
            catch (Exception exc)
            {
                throw new ApplicationException(exc.Message);
            }
        }
        public void ConnectUserAGS()
        {
            pList = new List<IAGSServerObjectName>();

            try
            {

                IPropertySet propertySet = new PropertySetClass();
                propertySet.SetProperty("url", Host);
                propertySet.SetProperty("ConnectionMode", esriAGSConnectionMode.esriAGSConnectionModeConsumer);
                //propertySet.SetProperty("ServerType", esriAGSServerType.esriAGSServerTypeDiscovery);
                propertySet.SetProperty("user", Username);
                propertySet.SetProperty("password", Password);
                //propertySet.SetProperty("ALLOWINSECURETOKENURL", true); 

                //打开连接

                // IAGSServerConnectionFactory pFactory = new AGSServerConnectionFactory();
                //Type factoryType = Type.GetTypeFromProgID(
                //    "esriGISClient.AGSServerConnectionFactory");
                //IAGSServerConnectionFactory agsFactory = (IAGSServerConnectionFactory)
                //    Activator.CreateInstance(factoryType);


                IAGSServerConnectionName3 pConnectName = new AGSServerConnectionNameClass() as IAGSServerConnectionName3;//10.1新增接口
                pConnectName.ConnectionProperties = propertySet;

                IAGSServerConnection pConnection = ((IName)pConnectName).Open() as IAGSServerConnection;

                //IAGSServerConnection pConnection = pFactory.Open(propertySet, 0);
                IAGSEnumServerObjectName pServerObjectNames = pConnection.ServerObjectNames;
                pServerObjectNames.Reset();

                IAGSServerObjectName agsServerObjectName = pServerObjectNames.Next();
                while (agsServerObjectName != null)
                {
                    pList.Add(agsServerObjectName);
                    agsServerObjectName = pServerObjectNames.Next();
                }



            }
            catch (Exception ex)
            {
                throw new SystemException(ex.Message);
            }
            //return pList;

        }
    }
}
